/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.examination.ticket.web;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.io.FileUtils;
import com.jeesite.common.utils.word.WordExport;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.Employee;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.service.EmpUserService;
import com.jeesite.modules.sys.service.EmployeeService;
import com.jeesite.modules.sys.utils.DictUtils;
import com.jeesite.modules.zx.examination.entity.Texamination;
import com.jeesite.modules.zx.examination.room.entity.TexaminationRoom;
import com.jeesite.modules.zx.examination.room.scene.entity.TexaminationRoomScene;
import com.jeesite.modules.zx.examination.room.scene.service.TexaminationRoomSceneService;
import com.jeesite.modules.zx.examination.room.service.TexaminationRoomService;
import com.jeesite.modules.zx.examination.service.TexaminationService;
import com.jeesite.modules.zx.examination.ticket.entity.TexaminationTicket;
import com.jeesite.modules.zx.examination.ticket.service.TexaminationTicketService;
import com.jeesite.modules.zx.examination.vip.entity.TexaminationVip;
import com.jeesite.modules.zx.examination.vip.service.TexaminationVipService;

/**
 * 准考证号Controller
 * @author zpp
 * @version 2020-03-22
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/examination/ticket/texaminationTicket")
public class TexaminationTicketController extends BaseController {

	@Autowired
	private TexaminationTicketService texaminationTicketService;
	@Autowired
	private TexaminationService texaminationService;
	@Autowired
	private TexaminationRoomService texaminationRoomService;
	@Autowired
	private EmpUserService empUserService;
	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private TexaminationVipService texaminationVipService;
	@Autowired
	private TexaminationRoomSceneService texaminationRoomSceneService;



	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TexaminationTicket get(String id, boolean isNewRecord) {
		return texaminationTicketService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:examination:ticket:texaminationTicket:view")
	@RequestMapping(value = {"list", ""})
	public String list(TexaminationTicket texaminationTicket, Model model) {
		model.addAttribute("texaminationTicket", texaminationTicket);
		return "modules/zx/examination/ticket/texaminationTicketList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:examination:ticket:texaminationTicket:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TexaminationTicket> listData(TexaminationTicket texaminationTicket, HttpServletRequest request, HttpServletResponse response) {
		texaminationTicket.setPage(new Page<>(request, response));
		Page<TexaminationTicket> page = texaminationTicketService.findPage(texaminationTicket);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:examination:ticket:texaminationTicket:view")
	@RequestMapping(value = "form")
	public String form(TexaminationTicket texaminationTicket, Model model) {
		model.addAttribute("texaminationTicket", texaminationTicket);
		return "modules/zx/examination/ticket/texaminationTicketForm";
	}
	@RequestMapping(value = "form2")
	public String form2(TexaminationTicket texaminationTicket, Model model) {
		
		String examId = texaminationTicket.getExamId();
		if(StringUtils.isNotBlank(examId)){
			Texamination texamination = texaminationService.get(examId);
			if(texamination!=null){
				texaminationTicket.setExamName(texamination.getObjName());
				texaminationTicket.setExamPoint(texamination.getExamPoint());
			}
		}
		String examRoom = texaminationTicket.getExamRoom();
		if(StringUtils.isNotBlank(examRoom)){
			TexaminationRoom texaminationRoom = texaminationRoomService.get(examRoom);
			if(texaminationRoom!=null){
				texaminationTicket.setExamRoom(texaminationRoom.getRoomName());
				texaminationTicket.setRoomAddress(texaminationRoom.getRoomAddress());
				texaminationTicket.setActualAddress(texaminationRoom.getActualAddress());
			}
		}
		String userId = texaminationTicket.getUserId();
		if(StringUtils.isNotBlank(userId)){
			EmpUser empUser = empUserService.get(userId);
			if(empUser!=null){
				texaminationTicket.setStudentName(empUser.getUserName());
				texaminationTicket.setIdCard(empUser.getIdCard());
				String refCode = empUser.getRefCode();
				if(StringUtils.isNotBlank(refCode)){
					Employee employee = employeeService.get(refCode);
					if(employee!=null){
						Office office = employee.getOffice();
						if(office!=null){
							String fullName = office.getFullName();
							texaminationTicket.setOfficeName(fullName);
							texaminationTicket.setOfficeId(office.getOfficeCode());
						}
					}
				}
				String sex = empUser.getSex();
				if(StringUtils.isNotBlank(sex)){
				  String dictLabel = DictUtils.getDictLabel("sys_user_sex", sex, "");
				  texaminationTicket.setSex(dictLabel);
				}
				texaminationTicket.setHeadPortrait(empUser.getAvatar());
			}
		}
		String scene=texaminationTicket.getScene();
		if(StringUtils.isNotBlank(scene)){
			TexaminationRoomScene texaminationRoomScene = texaminationRoomSceneService.get(scene);
			if(texaminationRoomScene!=null){
				texaminationTicket.setTheoryStarttime(texaminationRoomScene.getStartDate());
				texaminationTicket.setTheoryEndtime(texaminationRoomScene.getEndDate());
				texaminationTicket.setActualStarttime(texaminationRoomScene.getActualStarttime());
				texaminationTicket.setActualEndtime(texaminationRoomScene.getActualEndtime());
			}
			
		}
		model.addAttribute("texaminationTicket", texaminationTicket);
		return "modules/zx/examination/ticket/texaminationTicketForm";
	}
	/**
	 * 保存准考证号
	 */
	@RequiresPermissions("zx:examination:ticket:texaminationTicket:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TexaminationTicket texaminationTicket) {
		boolean isNewRecord = texaminationTicket.getIsNewRecord();
		if(isNewRecord){
			TexaminationTicket entity=new TexaminationTicket();
			entity.setExamId(texaminationTicket.getExamId());
			entity.setExamRoom(texaminationTicket.getExamRoom());
			entity.setScene(texaminationTicket.getScene());
			entity.setSeatNum(texaminationTicket.getSeatNum());
			List<TexaminationTicket> findList = texaminationTicketService.findList(entity);
			if(findList!=null&&findList.size()>0){
				return renderResult(Global.FALSE, text("座位号已经存在！"));
			}
		}
		texaminationTicketService.save(texaminationTicket);
		String vipId=texaminationTicket.getVipId();
		if(StringUtils.isNotBlank(vipId)){
			TexaminationVip tesvip=new TexaminationVip();
			tesvip.setId(vipId);
			tesvip.setTicketId(texaminationTicket.getId());
			texaminationVipService.update(tesvip);
		}
		return renderResult(Global.TRUE, text("保存准考证号成功！"));
	}
	
	/**
	 * 停用准考证号
	 */
	@RequiresPermissions("zx:examination:ticket:texaminationTicket:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TexaminationTicket texaminationTicket) {
		texaminationTicket.setStatus(TexaminationTicket.STATUS_DISABLE);
		texaminationTicketService.updateStatus(texaminationTicket);
		return renderResult(Global.TRUE, text("停用准考证号成功"));
	}
	
	/**
	 * 启用准考证号
	 */
	@RequiresPermissions("zx:examination:ticket:texaminationTicket:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TexaminationTicket texaminationTicket) {
		texaminationTicket.setStatus(TexaminationTicket.STATUS_NORMAL);
		texaminationTicketService.updateStatus(texaminationTicket);
		return renderResult(Global.TRUE, text("启用准考证号成功"));
	}
	
	/**
	 * 删除准考证号
	 */
	@RequiresPermissions("zx:examination:ticket:texaminationTicket:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TexaminationTicket texaminationTicket) {
		texaminationTicketService.delete(texaminationTicket);
		return renderResult(Global.TRUE, text("删除准考证号成功！"));
	}


	/**
	 * 下载准考证
	 * @throws IOException 
	 */

	@RequestMapping(value = "download")
	@ResponseBody
	public String downTexaminationTicker(TexaminationTicket texaminationTicket,HttpServletRequest request,HttpServletResponse response){
//        WordExport
		String file=texaminationTicketService.downTexaminationTicker(texaminationTicket);
		System.out.println(file);
		return renderResult(Global.TRUE, text("下载准考证号成功！"));
	}
	
}